package com.qf.service.impl;

import com.qf.dao.DetailsDao;
import com.qf.dao.impl.DetailsDaoImpl;
import com.qf.entity.Details;
import com.qf.service.DetailsService;
import com.qf.util.DruidUtil;
import com.qf.util.Page;

import java.util.List;

/*
杨威
2021/9/24
20:00
*/
public class DetailsServiceImpl implements DetailsService {
    DetailsDao detailsDao=new DetailsDaoImpl();
    @Override
    public List<Details> showAllByPage(Page page) {
        List<Details> detailsList = detailsDao.selectAll(page);
        Long count = detailsDao.selectCount();
        page.setTotalCount(count);
        DruidUtil.close();
        return detailsList;
    }

    @Override
    public int addDetails(Details details, Integer companyId) {
        int insert=0;
        try {
            DruidUtil.begin();
            insert = detailsDao.insert(details, companyId);
            DruidUtil.commit();
        } catch (Exception e) {
            e.printStackTrace();
            DruidUtil.rollback();
        } finally {
            DruidUtil.close();
        }
        return insert;
    }

    @Override
    public Details selectDetailsById(Integer id) {
        Details details = detailsDao.selectById(id);
        DruidUtil.close();
        return details;
    }

    @Override
    public int updateDetails(Details details, Integer companyId) {
        int i = 0;
        try {
            DruidUtil.begin();
            i = detailsDao.updateDetails(details, companyId);
            DruidUtil.commit();
        } catch (Exception e) {
            DruidUtil.rollback();
            e.printStackTrace();
        } finally {
            DruidUtil.close();
        }
        return i;
    }

    @Override
    public int deleteDetails(Integer id) {
        int delete = 0;
        try {
            DruidUtil.begin();
            delete = detailsDao.delete(id);
            DruidUtil.commit();
        } catch (Exception e) {
            DruidUtil.rollback();
            e.printStackTrace();
        } finally {
            DruidUtil.close();
        }
        return delete;
    }
}
